﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SKDB_Matcher.SQL.Database;
using System.Threading;

namespace SKDB_Matcher
{
    public partial class CChooseDatabase : Form
    {
        String m_DatabaseName;

        int m_ConnectionState = 0;

        public const int CONNECTION_ERROR = 2;
        public const int CONNECTION_UNKNOW = 0;
        public const int CONNECTION_OK = 1;

        public String DatabaseName
        {
            get { return m_DatabaseName; }
            set { m_DatabaseName = value;
            tbDatabaseName.Text = m_DatabaseName;
            }
        }

        public CChooseDatabase()
        {
            InitializeComponent();
            SetConnectionState(CONNECTION_UNKNOW);
            
        }

        CWaitingDlg temp = null;

        private void btCheck_Click(object sender, EventArgs e)
        {
            if (m_DatabaseName != null && m_DatabaseName.Length > 0)
            {
                labelCheck.Text = "Verbindung wird getestet";

                temp = new CWaitingDlg();
                temp.Show();
    
                new Thread(new ThreadStart(check)).Start();
                
            }
        }

        private void check()
        {
            if (CDatabaseHelper.CheckDatabaseConnection(m_DatabaseName))
                SetConnectionState(CONNECTION_OK);
            else
                SetConnectionState(CONNECTION_ERROR);
           
        }

       private void SetConnectionState(int _State)
        {
            if (labelCheck.InvokeRequired)
            {
                labelCheck.Invoke(new MethodInvoker(() => { SetConnectionState(_State); }));
            }
            else
            {
                if (temp != null)
                {
                    temp.Close();
                    temp = null;
                }
                
                m_ConnectionState = _State;
                switch (m_ConnectionState)
                {
                    case CONNECTION_ERROR:
                        {
                            labelCheck.Text = "Verbindungsfehler!";
                            break;
                        }
                    case CONNECTION_UNKNOW:
                        {
                            labelCheck.Text = "Verbindung ungeprüft!";
                            break;
                        }
                    case CONNECTION_OK:
                        {
                            labelCheck.Text = "Verbindung erfolgreich!";
                            break;
                        }
                }
                pbCheck.Image = imageList1.Images[_State];
            }
        }

        private void tbServer_TextChanged(object sender, EventArgs e)
        {
            m_DatabaseName = tbDatabaseName.Text;
            SetConnectionState(CONNECTION_UNKNOW);
        }

        private void btAccept_Click(object sender, EventArgs e)
        {
            if (m_ConnectionState == CONNECTION_OK)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else
            {
                MessageBox.Show("Please check connection first!", "Check connection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
